package com.zhentao.service.impl;

import com.alibaba.fastjson2.JSONObject;

import com.zhentao.pojo.WxProperties;
import com.zhentao.service.WxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author tianshuaifei
 * @data 2024/7/8 15:36
 */

@Service
public class WxServiceImpl implements WxService {


    @Resource
    RestTemplate restTemplate;

    @Resource
    WxProperties wxProperties;

    @Override
    public String getAccessToken() {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";
        String accessUrl = String.format(url,wxProperties.getAppid(),wxProperties.getAppsecret());
        JSONObject forObject = restTemplate.getForObject(accessUrl, JSONObject.class);
        String accessToken = forObject.get("access_token").toString();
        return accessToken;
    }
}
